﻿using System;
using System.Collections;
using GeoAPI.Geometries;
using YJKGridTopologyCal.Algorithm;
using YJKGridTopologyCal.GeometriesGraph;
using YJKGridTopologyCal.GeometriesGraph.Index;
using YJKGridTopologyCal.Operation.Relate;

namespace YJKGridTopologyCal.Operation.Valid
{
	public class ConsistentAreaTester
	{
		public ConsistentAreaTester(GeometryGraph geomGraph)
		{
			this.geomGraph = geomGraph;
		}

		public Coordinate InvalidPoint
		{
			get
			{
				return this.invalidPoint;
			}
		}

		public bool IsNodeConsistentArea
		{
			get
			{
				SegmentIntersector segmentIntersector = this.geomGraph.ComputeSelfNodes(this.li, true);
				if (segmentIntersector.HasProperIntersection)
				{
					this.invalidPoint = segmentIntersector.ProperIntersectionPoint;
					return false;
				}
				this.nodeGraph.Build(this.geomGraph);
				return this.IsNodeEdgeAreaLabelsConsistent;
			}
		}

		private bool IsNodeEdgeAreaLabelsConsistent
		{
			get
			{
				IEnumerator nodeEnumerator = this.nodeGraph.GetNodeEnumerator();
				while (nodeEnumerator.MoveNext())
				{
					object obj = nodeEnumerator.Current;
					RelateNode relateNode = (RelateNode)obj;
					if (!relateNode.Edges.IsAreaLabelsConsistent(this.geomGraph))
					{
						this.invalidPoint = (Coordinate)relateNode.Coordinate.Clone();
						return false;
					}
				}
				return true;
			}
		}

		public bool HasDuplicateRings
		{
			get
			{
				IEnumerator nodeEnumerator = this.nodeGraph.GetNodeEnumerator();
				while (nodeEnumerator.MoveNext())
				{
					object obj = nodeEnumerator.Current;
					foreach (object obj2 in ((RelateNode)obj).Edges)
					{
						EdgeEndBundle edgeEndBundle = (EdgeEndBundle)obj2;
						if (edgeEndBundle.EdgeEnds.Count > 1)
						{
							this.invalidPoint = edgeEndBundle.Edge.GetCoordinate(0);
							return true;
						}
					}
				}
				return false;
			}
		}

		private readonly LineIntersector li = new RobustLineIntersector();

		private readonly GeometryGraph geomGraph;

		private readonly RelateNodeGraph nodeGraph = new RelateNodeGraph();

		private Coordinate invalidPoint;
	}
}
